import axios from "axios"
import { ElMessage } from "element-plus";
import router from '@/router';

const request = axios.create({
  baseURL: '/api',
  timeout: 100000,
  // headers: {'X-Custom-Header': 'foobar'}
})

request.interceptors.response.use((response) =>{
    return response.data;
  }, function (error) {
    ElMessage.error(error.message) 
    if(error.response.status===401){
      // ElMessage.error('登录失效请重新登录！') 
      router.push('/login')
    }
    return Promise.reject(error);
  });


request.interceptors.request.use((config)=>{
  const token = localStorage.getItem("token")
  if(token){
    config.headers.token = JSON.parse(token)
  }
  return config;
},
(error)=>{
  return Promise.reject(error);
})
export default request